<?php

/**
 * books.php
 *
 * @author vbolshov
 */

include dirname(__FILE__) . '/../setup.php';

use bs\Author;
use bs\Renderer;

$authors = Author::findAll();
$sort = in_array(@$_GET['sort'], array('id', 'name')) ? $_GET['sort'] : 'id';
$dir = in_array(@$_GET['dir'], array('asc', 'desc')) ? $_GET['dir'] : 'asc';

$authors->order($sort . ' ' . $dir);

if (@$_SERVER['HTTP_X_REQUESTED_WITH'])
{
	$authors->setFetchMode(PDO::FETCH_ASSOC);
	echo json_encode($authors->fetchAll());
	exit;
}

$layout = Renderer::getLayout('authors.php');
$layout->title = 'Bookstore: manage author list';
$layout->sort = $sort;
$layout->sort_dir = $dir;

$layout->authors = $authors;

$layout->render();